<template>
  <div>
      <template v-if="!item.hidden">

        <FLink v-if="hasOneShowingChild(item.children, item) && (!onlyOneChild.children)" :to="resolvePath(onlyOneChild.path)">
          <el-menu-item  :index="resolvePath(onlyOneChild.path)">
            <i v-if="onlyOneChild.meta.icon" :class="onlyOneChild.meta.icon"></i>
            <span slot="title">{{onlyOneChild.meta.title }}</span>
          </el-menu-item>
        </FLink>

        <el-submenu v-else :index="resolvePath(item.path)">
          <template slot="title">
            <i v-if="item.meta.icon" :class="item.meta.icon"></i>
            <span slot="title">{{item.meta.title}}</span>
          </template>

           <FSidebar  v-for="(child, index) in item.children" :key="'menu' + index" :item="child" :basePath="resolvePath(child.path)"></FSidebar>
        </el-submenu>

      </template>

  </div>
</template>

<script>
import {isExternal} from '@/utils/validate'
import path from 'path'
import FLink from '@/components/f-link/f-link'

export default {
  name: 'FSidebar',
  components: {
    FLink
  },
  data () {
    this.onlyOneChild = null
    return {}
  },
  props: { // 默认选中
    item: {// 菜单数据格式
      type: Object,
      required: true
    },
    basePath: {
      type: String,
      default: ''
    }
  },
  methods: {
    hasOneShowingChild (children = [], parent) {
      const showingChildren = children.filter(item => {
        if (item.hidden) {
          return false
        } else {
          // Temp set(will be used if only has one showing child)
          this.onlyOneChild = item
          return true
        }
      })

      // When there is only one child router, the child router is displayed by default
      if (showingChildren.length === 1) {
        return true
      }

      // Show parent if there are no child router to display
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: '', noShowingChildren: true }
        return true
      }

      return false
    },
    resolvePath (routePath) {
      if (isExternal(routePath)) {
        return routePath
      }
      if (isExternal(this.basePath)) {
        return this.basePath
      }
      return path.resolve(this.basePath, routePath)
    }
  }
}
</script>

<style scoped>

</style>
<style>
  .el-menu{
    /*text-align: left;*/
  }
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 250px;
    min-height: 400px;
  }

  .el-menu--horizontal > div{
    display: inline-flex;
  }
  /* 水平样式 */
  .el-menu--horizontal > div > .el-submenu {
    float: left;
  }

  /* 一级菜单的样式 */
  .el-menu--horizontal > div > .el-menu-item {
    float: left;
    height: 56px;
    line-height: 56px;
    margin: 0;
    border-bottom: 2px solid transparent;
    color: #909399;
  }

  /* 解决下图1 下拉三角图标 */
  .el-menu--horizontal > div > .el-submenu .el-submenu__icon-arrow {
    position: static;
    vertical-align: middle;
    margin-left: 8px;
    margin-top: -3px;
  }

  /* 解决下图2 无下拉菜单时 不对齐问题 */
  .el-menu--horizontal > div > .el-submenu .el-submenu__title {
    height: 56px;
    line-height: 56px;
    border-bottom: 2px solid transparent;
    color: #000000;
  }

  .el-menu--collapse > div > .el-menu-item span, .el-menu--collapse > div > .el-submenu > .el-submenu__title span {
    height: 0;
    width: 0;
    overflow: hidden;
    visibility: hidden;
    display: inline-block;

  }
  .el-menu--collapse > div > .el-menu-item .el-submenu__icon-arrow, .el-menu--collapse > div > .el-submenu > .el-submenu__title .el-submenu__icon-arrow {
    display: none
  }
</style>
